<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_logo_box">
        <img src="../assets/logo.png" />
      </div>
      <el-form
        class="login_form"
        :model="loginForm"
        :rules="loginRules"
        ref="loginRef"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="login_btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
        grant_type: "password",
        client_id: "yxw",
        client_secret: "appyxw",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 5,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    reset() {
      this.$refs.loginRef.resetFields();
    },
    login() {
      debugger;
      this.$refs.loginRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error("预校验不通过");
        }
        const { data: res } = await this.$http.post(
          "/auth/oauth/token",
          this.loginForm
        );
        debugger;
        if (!res.success) {
          return this.$message.error(res.message);
        }
        this.$message.success("登录成功");
        window.sessionStorage.setItem("token", res.entity.access_token);
        window.sessionStorage.setItem("currentLoginInfo", [
          JSON.stringify(res.entity.user),
        ]);
        this.$router.push("/home");
      });
    },
  },
};
</script>
<style scoped>
.login_container {
  height: 100%;
  background-color: #b3c0d1;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.login_logo_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background-color: #eee;
}
.login_form {
  width: 100%;
  position: absolute;
  bottom: 0%;
  padding: 0 20px;
  box-sizing: border-box;
}
.login_btns {
  display: flex;
  justify-content: flex-end;
}
</style>
